package edu.ccut.saturn.component.impl.sql;

import java.util.Map;

import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.utils.TypeValidation;

public class IsLessThanItemInfo extends AbstractDyadicExprSqlItemInfo {

	public IsLessThanItemInfo(String prepend, String property,
			String comparedValue, boolean isProperty) {

		super(prepend, property, comparedValue, isProperty);
	}

	@Override
	public String getSubSql(Map<String, Object> param) throws Exception {

		if (this.getProperty() != null && this.getComparedValue() != null) {
			Double leftValue = null;
			Double rightValue = null;

			try {
				leftValue = (Double) TypeValidation.getValidTypeValue("Double",
						param.get(this.getProperty()));

				if (isProperty()) {
					rightValue = (Double) TypeValidation.getValidTypeValue(
							"Double", param.get(this.getComparedValue()));

				} else {
					rightValue = (Double) TypeValidation.getValidTypeValue(
							"Double", this.getComparedValue());
				}

			} catch (NumberFormatException exception) {
				throw new SaturnComponentException("");
			}

			if (leftValue != null && rightValue != null) {

				if (leftValue < rightValue) {
					return parseSql(param);

				} else {
					return " ";
				}
			}
		}
		throw new SaturnComponentException("");
	}

	@Override
	public String getType() {
		return "isLessThan";
	}
}
